package cn.superchart.module.dev.template.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.superchart.common.base.BaseServiceImpl;
import cn.superchart.common.exception.ScException;
import cn.superchart.module.dev.api.utils.FreemarkerHelper;
import cn.superchart.module.dev.template.entity.ScTemp;
import cn.superchart.module.dev.template.mapper.ScTempMapper;
import cn.superchart.module.dev.template.param.*;
import cn.superchart.module.dev.template.service.ScTempService;
import cn.superchart.module.dev.tempparam.entity.ScTempParam;
import cn.superchart.module.dev.tempparam.service.ScTempParamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ScTempServiceImpl extends BaseServiceImpl<ScTemp,ScTempMapper> implements ScTempService {

    @Resource
    private ScTempParamService scTempParamService;

    @Resource
    private FreemarkerHelper freemarkerHelper;


    @Override
    public Page<ScTemp> page(ScTempPageParam ScTempPageParam) {
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ScTempAddParam ScTempAddParam) {
        ScTemp ScTemp = BeanUtil.toBean(ScTempAddParam, ScTemp.class);
        checkCode(ScTempAddParam.getTempCode(),null);
        this.save(ScTemp);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(ScTempEditParam ScTempEditParam) {
        ScTemp ScTemp = this.queryEntity(ScTempEditParam.getId());
        BeanUtil.copyProperties(ScTempEditParam, ScTemp);
        checkCode(ScTempEditParam.getTempCode(),ScTemp.getId());
        this.updateById(ScTemp);
    }

    private void checkCode(String code,String id){
        QueryWrapper<ScTemp> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ScTemp::getTempCode,code).ne(ObjectUtil.isNotNull(id),ScTemp::getId,id);
        long count = super.count(queryWrapper);
        if(count>0){
            throw new ScException("证书编码已存在：{}", code);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<ScTempIdParam> ScTempIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(ScTempIdParamList, ScTempIdParam::getId));
    }

    @Override
    public ScTemp detail(ScTempIdParam ScTempIdParam) {
        return this.queryEntity(ScTempIdParam.getId());
    }

    @Override
    public ScTemp queryEntity(String id) {
        ScTemp ScTemp = this.getById(id);
        if(ObjectUtil.isEmpty(ScTemp)) {
            throw new ScException("证书模板表不存在，id值为：{}", id);
        }
        return ScTemp;
    }

    @Override
    public void setContent(ScTempContentParam ScTempContentParam) {
         String content = ScTempContentParam.getContent();
         if(ObjectUtil.isNotEmpty(content)){
             baseMapper.setContent(ScTempContentParam.getTempId(),ScTempContentParam.getContent());
         }
    }

    @Override
    public String previewContent(ScTempContentParam previewTempContentParam) {
        String content = previewTempContentParam.getContent();
        String tempId = previewTempContentParam.getTempId();
        Map<String,Object> params = previewTempContentParam.getParams();
        boolean isOk = validParam(tempId,params);
        if(isOk){
            Map<String,String> tempLoaderMap = new HashMap<>();
            tempLoaderMap.put(tempId,content);
            freemarkerHelper.initTemplateLoader(tempLoaderMap);
            String outHtmlStr =  freemarkerHelper.outString(tempId,params);
            return outHtmlStr;
        }
        return "模板参数异常请检查参数是否正确";
    }

    @Override
    public String getContent(String id) {
        return baseMapper.getContent(id);
    }

    @Override
    public ScTempDetailParam info(ScTempIdParam ScTempIdParam) {
        String tempId = ScTempIdParam.getId();
        ScTempDetailParam tempDetailParam = new ScTempDetailParam();
//        List<ScTempParam> paramList = ScTempParamService.listByTempId(tempId);
//        tempDetailParam.setTempParamList(paramList);
        tempDetailParam.setContent(getContent(tempId));
        return tempDetailParam;
    }



    private boolean validParam(String tempId, Map<String,Object> params){
        return true;
    }


    // 校验必填参数
    private boolean validRequiredParam(List<ScTempParam>  paramList, Map<String,Object> params){
        return true;
    }


    private boolean validTypeParam(List<ScTempParam> paramList,Map<String,Object> params){
        return true;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        String v = "222";
        System.out.println(v.getClass());
        Class c = Class.forName("java.lang.String");
        System.out.println(c.equals(v.getClass()));
    }
}
